/**
 * 主功能页面
 * author:zhushy
 * date:2016年3月9日21:30:42
 */
;
define(function(require, exports, module) {
	var wechat = require("wechat");
	var template = require("template");
	var iscroll = require("iscroll");
	var index = {
		data: {},
		wxUserInfoObj: {},
		recommEnable: true,
		/**
		 * bindEvent 页面事件，要先用unbind解绑
		 */
		bindEvent: function() {
			$("#menMana").unbind().on("click", function() {
				var permissions = window.localStorage.getItem("permissions");
				if(permissions == "0") {
					window.location.href = "#/index/Management";
				} else if(permissions == "1") {
					window.location.href = "#/index/managementSup";
				}else{
					window.location.href = "#/index/login";
				}
			});
			$("#tonew").unbind().on("click", function() {
				window.localStorage.removeItem("userInfo");
				window.localStorage.setItem("update", "新增");
				window.localStorage.setItem("addedItem","");
				window.localStorage.setItem("bianji","0")
				window.location.href = "#/index/newmenber";
			});
		},
		/**
		 * 进行页面路由监听配置
		 * 
		 */
		bindRout: function() {
			//公共页面监听路由
			Path.map("#/index/:page_name").to(function() {
				console.log(this.params["page_name"]);
				var page_name = this.params["page_name"];
				var nextPageFrom = "right"; //从右边过来
				//记录状态
				common.loadPage({
					pageUrl: "html/" + page_name + ".html",
					reload: true,
					from: "none"
				});
			});

			//Path.root("#home");
			Path.listen();
		},
		/**
		 * 页面数据加载
		 */
		initData: function() {
			var loginstr = window.localStorage.getItem("loginmessage");
			var loginObj = JSON.parse(loginstr);
			console.log(loginObj);
			if(loginObj.permissions == "1") {
				$("#pending").removeClass("hidden");
				$("#tonew").remove();
				$(".sp_cont").text(loginObj.count);
			} else if(loginObj.permissions == "0") {
				//什么都不做
			} else {
				common.showTip({
					content: loginObj.message
				});
			}
			index.sendParam();
			//			var pwd = window.localStorage.getItem("pwd");
			//			var param = {
			//					usercode: usercode,
			//					pwd:pwd
			//		           }
			//		common.loadData({
			//			url: "/service/RRTPubServlet?method=login", //"/UMPPubServlet?method=getWxUserInfo",
			//			param: param,
			//			successCallBack: index.IndexCallBack, //回调函数
			//			dataSource: "Login",
			//			cache: false
			//		});
		},
		sendParam: function(){
			var usercode = window.localStorage.getItem("usercode");
			var pwd = window.localStorage.getItem("pwd");
			var param = {
					usercode: usercode,
					pwd:pwd
		        }
			common.loadData({
				url: "/service/RRTPubServlet?method=login", //"/UMPPubServlet?method=getWxUserInfo",
				param: param,
				successCallBack: index.IndexCallBack, //回调函数
				dataSource: "Login",
				cache: false
			});
		},
		IndexCallBack: function(json){
			console.log(JSON.stringify(json));
			if(json.status == "0") {
				$(".sp_cont").text(json.data.count);
			} else {
				common.showTip({
					content: json.message
				});
			}

		},
		//callback function
		//		IndexCallBack: function(json) {
		//			console.log(JSON.stringify(json));
		//			if(json.status == "0") {
		//				console.log(json.data.count);
		//				if(json.data.permissions == "1"){
		//					$("#pending").removeClass("hidden");
		//					$(".sp_cont").text(json.data.count);
		//				}else if(json.data.permissions == "0"){
		//					//什么都不做
		//				}
		//				
		//			} else {
		//				common.showTip({
		//					content: json.message
		//				});
		//			}
		//			common.initIscroll({
		//				container: "iscrollContent_index",
		//				showScrollBar: false,
		//				index: 1
		//			});
		//
		//		},
		/**
		 * each page init mothed
		 */
		initialize: function() {
			//动态加载数据
			index.initData();
			//事件绑定
			index.bindEvent();
			//路由绑定
			index.bindRout();
		}
	};

	module.exports = index;
});